
from whiff.middleware import misc
import config
from proxyCanvas import Canvas
import infoSchema

class drawSchema(misc.utility):
    def __init__(self,
                 schemaJson,
                 canvasId,
                 eventListenerVariable,
                 clickCallback = "",
                 minWidth=300,
                 minHeight=300
                 ):
        self.schemaJson = schemaJson
        self.canvasId = canvasId
        self.clickCallback = clickCallback
        self.eventListenerVariable = eventListenerVariable
        self.minWidth = minWidth
        self.minHeight = minHeight
    def __call__(self, env, start_response):
        schemaJson = self.param_json(self.schemaJson, env)
        canvasId = self.param_value(self.canvasId, env).strip()
        clickCallback = self.param_value(self.clickCallback, env).strip()
        eventListenerVariable = self.param_value(self.eventListenerVariable, env).strip()
        minWidth = self.param_json(self.minWidth, env)
        minHeight = self.param_json(self.minHeight, env)
        c = Canvas()
        cfg = config.Configuration()
        S = infoSchema.schemaFromJsonValue(schemaJson)
        S.dumpToCanvas(c, cfg)
        program = c.programText(canvasId, minWidth, minHeight, clickCallback, eventListenerVariable)
        start_response("200 OK", [('Content-Type', 'application/javascript')])
        return [program]

__middleware__ = drawSchema

TESTTEMPLATE = """
<HTML>
<HEAD>

<script type="text/javascript" src="boxed.js"></script>

<script type="text/javascript">

function canvasClickCallback(data, x, y, canvas, ev) {
         ////al("callback "+data+", "+x+", "+y);
         var promptDiv = document.getElementById("promptDiv");
         promptDiv.innerHTML = "callback "+data+", "+x+", "+y;
         promptDiv.style.visibility = "visible";
}


function setUpCanvas() {
%s
}

</script>
</HEAD>
<BODY onload="setUpCanvas()">
<h3> canvas test page</h3>

<table border><tr><td>
<div id="container">

<canvas id="testCanvas" width="100" height="100">
canvas tag not supported?
</canvas>
</div>
</td></tr></table>

<div id="promptDiv" class="jswhiff_suggestion" style="width:400px; visibility:hidden">
mouse event prompt will be displayed here
</div>


<h3>end of test page</h3>
</BODY>
</HTML>

"""

if __name__=="__main__":
    canvasId='testCanvas'
    clickCallback='canvasClickCallback'
    schemaJson ={
    'Proj': [ 
        {'name':'pnum','typeName': 'int', 'isIdentifier':True} ,
        {'name':'mgr','typeName': 'Emp'},
        {'name': 'partOf', 'typeName':'Proj'},
        {'name': 'within', 'typeName':'Dept'},
        ],
    'Status': [ 
        {'name':'category','typeName': 'str', 'isIdentifier':True} ,
        {'name':'minsalary','typeName': 'currency'},
        {'name':'maxsalary','typeName': 'currency'},
        ],
    'Dept': [ 
        {'name':'dnum','typeName': 'int', 'isIdentifier':True} , 
        {'name':'mgr','typeName': 'Emp'}
        ],
    'Emp': [ 
        {'name':'enum','typeName': 'int', 'isIdentifier': True} ,
        {'name':'category','typeName': 'Status'}
        ],
    'WorksOn': [
        {'name':'worker','typeName': 'Emp', 'isIdentifier':True} ,
        {'name':'activity','typeName': 'Proj', 'isIdentifier':True} ,
        ],
    'WorksFor': [
        {'name':'worker','typeName': 'Emp', 'isIdentifier':True} ,
        {'name':'group','typeName': 'Dept', 'isIdentifier':True} ,
        ],
    }
    app = drawSchema(schemaJson, canvasId, clickCallback)
    iterable = app({}, misc.ignore)
    program = "".join(list(iterable))
    print TESTTEMPLATE % program

